import { ifLogined, isEmptyObject } from '../../utils/util.js'
const config = getApp().globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
    rankList: [],
    COUNT: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getEntryByRank(true)
  },

  getEntryByRank(reload) {
    const auth = ifLogined()
    let rankList = this.data.rankList
    if (isEmptyObject(rankList) || reload) {
      rankList = [{ rankIndex: '' }]
    }
    let rankIndex = (rankList.slice(-1)[0].rankIndex) || ''
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: `${config.config.timelineRequestUrl}/get_entry_by_rank`,
      data: {
        src: 'web',
        uid: auth.uid || 'unlogin',
        device_id: auth.clientId,
        token: auth.token || '',
        limit: this.data.COUNT,
        // category: 'all',
        // recomment: 1,
        before: rankIndex,
      },
      success: res => {
        wx.hideLoading()
        let data = res.data
        if (data.s === 1) {
          wx.hideLoading()
          let entrylist = (data.d && data.d.entrylist) || []
          this.setData({
            rankList: reload ? entrylist : this.data.rankList.concat(entrylist),
          })
        } else {
          wx.showToast({
            title: data.m.toString(),
            icon: 'none',
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '网路开小差，请稍后再试',
          icon: 'none',
        })
      },
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },
})